#include "feature.h"
#include "background.h"
#include "examples.h"
#include "scenario.h"
#include "step.h"
#include "step_definition.h"
#include "table.h"
#include <stdio.h>

using namespace CppStory;

Feature::Feature(const char* f, const int l)
: file(f)
, line(l)
, cur_scenario(NULL)
, background(NULL)
{
}

Feature::~Feature()
{
    if (NULL != background)
        delete background;

    std::vector< Scenario* >::iterator scenario_iter = scenarios.begin();
    while (scenario_iter != scenarios.end())
    {
        delete *scenario_iter;
#ifdef WIN32
        scenario_iter = scenarios.erase(scenario_iter);
#else
        scenarios.erase(scenario_iter);
#endif
    }
}

void Feature::register_scenario(Scenario *s)
{
    scenarios.push_back(s);
    cur_scenario = s;
}

void Feature::create_background()
{
    background = new Background();
}

bool Feature::run()
{
    bool ret = true;
    std::vector< Scenario* >::iterator scenario_iter;
    for (scenario_iter = scenarios.begin();
         scenario_iter != scenarios.end();
         ++scenario_iter)
    {
        bool background_ret = true;
        if (NULL != get_background())
            background_ret = get_background()->run();

        if(background_ret && (*scenario_iter)->run())
            successful_scenarios.push_back(*scenario_iter);
        else
        {
            failed_scenarios.push_back(*scenario_iter);
            ret = false;
        }

        if (NULL != get_background())
            get_background()->clean_contexts();
    }
    if (0 == scenarios.size())
        ret = false;
    return ret;
}

